﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    /// <summary>
    /// 此类用于主界面左工具栏中的“最近工作区列表”，每个实例都表示一个最近的工作区。
    /// </summary>
    public class RecentDirectoryListBoxItem : ListBoxItem
    {
        /// <summary>
        /// [构造方法]表示一个最近工作区目录。
        /// </summary>
        /// <param name="directoryPath">目录路径。</param>
        public RecentDirectoryListBoxItem(string directoryPath)
        {
            this.DirectoryPath = directoryPath;

            this.image.Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/FolderHS.png"));
            this.image.Margin = new System.Windows.Thickness(0, 0, 4, 0);

            this.basePanel.Children.Add(image);
            DockPanel.SetDock(this.image, Dock.Left);
            this.basePanel.Children.Add(indexHeader);
            DockPanel.SetDock(this.indexHeader, Dock.Left);

            var stackPanel = new StackPanel() { Orientation = Orientation.Vertical, };
            stackPanel.Children.Add(this.headerTextBlock);
            stackPanel.Children.Add(this.comment);

            this.basePanel.Children.Add(stackPanel);

            this.Content = this.basePanel;
        }

        /// <summary>
        /// 指向某个曾被用作工作区的磁盘目录的路径。
        /// </summary>
        public string DirectoryPath
        {
            get { return this.comment.Text; }
            set
            {
                if (Directory.Exists(value))
                {
                    DirectoryInfo di = new DirectoryInfo(value);

                    if (di.FullName == Globals.DefaultWorkspacePath)
                    {
                        this.headerTextBlock.Text = "<备用文件源工作区>";
                    }
                    else
                    {
                        //this.headerTextBlock.Text = di.Name;
                        var metaFileFullPath = di.FullName.EndsWith("\\") ? di.FullName : (di.FullName + "\\");
                        metaFileFullPath += "_" + di.Name + ".md";

                        if (Globals.MainWindow.ShowTitleInWorkspaceManager && File.Exists(metaFileFullPath))
                        {
                            this.headerTextBlock.Text = MainWindow.GetTitleOfMdFile(metaFileFullPath);
                        }
                        else
                        {
                            this.headerTextBlock.Text = di.Name;
                        }
                    }
                    this.comment.Text = di.FullName;
                }
                else
                {
                    this.headerTextBlock.Text = "<非法路径>";
                    this.comment.Text = "文件不存在...";
                }
            }
        }

        /// <summary>
        /// 此条目在“最近工作区”列表中应显示的序号。
        /// </summary>
        public string IndexText
        {
            set { this.indexHeader.Text = value; }
        }

        /// <summary>
        /// 用以显示此条目在“最近工作区”列表中的序号文本。
        /// </summary>
        private TextBlock indexHeader = new TextBlock()
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            FontWeight = FontWeights.Bold,
            FontFamily = new FontFamily("SimSun"),
            FontSize = 24,
            Margin = new Thickness(4, 2, 4, 2),
            Padding = new Thickness(4, 0, 4, 0),
        };

        /// <summary>
        /// 用以承载图标、序号文本块、标题文本块（显示文件夹短名）、注释文本块（显示文件夹完整路径）。
        /// </summary>
        private DockPanel basePanel = new DockPanel() { LastChildFill = true, };

        private TextBlock headerTextBlock = new TextBlock()
        {
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Foreground = Brushes.Black,
        };
        /// <summary>
        /// 用以呈现此条目指向的磁盘目录的短名。
        /// </summary>
        public TextBlock HeaderTextBlock
        {
            get { return headerTextBlock; }
            set { headerTextBlock = value; }
        }

        /// <summary>
        /// 最近工作区列表条目在文件夹短名下侧显示文件夹完整路径。
        /// </summary>
        private TextBlock comment = new TextBlock()
        {
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Foreground = Brushes.Black,
            Padding = new Thickness(14, 0, 0, 0),
        };

        /// <summary>
        /// 工作区列表条目左侧显示的文件夹小图标。
        /// </summary>
        private Image image = new Image() { Width = 16, Height = 16, VerticalAlignment = System.Windows.VerticalAlignment.Center, };
    }
}
